package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.FlashPromotion;
import com.itheima.domain.FlashPromotionProductRelation;
import com.itheima.mapper.FlashPromotionMapper;
import com.itheima.mapper.FlashPromotionProductRelationMapper;
import com.itheima.service.SmsFlashPromotionService;
import com.itheima.vo.PageBeanVo;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsFlashPromotionServiceImpl implements SmsFlashPromotionService {

    @Autowired
    FlashPromotionMapper smsFlashPromotionMapper;
    @Autowired
    FlashPromotionProductRelationMapper relationMapper;

    //根据活动名称分页查询
    @Override
    public PageBeanVo flashByPage(Integer pageNum, Integer pageSize, String title) {

        Page<FlashPromotion> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<FlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(title), FlashPromotion::getTitle, title).orderByDesc(FlashPromotion::getCreateTime);
        ;

        Page<FlashPromotion> pageInfo = smsFlashPromotionMapper.selectPage(page, lqw);

        return new PageBeanVo(pageNum, pageSize, pageInfo.getTotal(), pageInfo.getRecords());
    }

    //添加活动
    @Override
    public void save(FlashPromotion smsFlashPromotion) {
        smsFlashPromotionMapper.insert(smsFlashPromotion);
    }

    //删除活动
    @Override
    public void delete(Integer id) {
        LambdaQueryWrapper<FlashPromotion> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlashPromotion::getId, id);
        smsFlashPromotionMapper.delete(lqw);
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(FlashPromotionProductRelation::getFlashPromotionId, id);
        relationMapper.delete(lqw2);
    }

    //修改上下线状态
    //编辑活动
    @Override
    public void updateById(FlashPromotion smsFlashPromotion) {
        smsFlashPromotionMapper.updateById(smsFlashPromotion);
    }

    //获取活动详情
    @Override
    public FlashPromotion selectById(Integer id) {
        FlashPromotion smsFlashPromotion = smsFlashPromotionMapper.selectById(Long.valueOf(id));
        return smsFlashPromotion;
    }

}
